<?php 
require get_template_directory() . '/functions/disable.php';
require get_template_directory() . '/functions/comments.php';
require get_template_directory() . '/functions/postviews.php';
require get_template_directory() . '/functions/news.php';
require get_template_directory() . '/functions/widget-new.php';
require get_template_directory() . '/functions/widget-hot.php';
require get_template_directory() . '/include/ashuwp_framework_core.php';
require get_template_directory() . '/include/config.php';

function get_term_parents( $id, $link = false, $separator = '/', $nicename = false, $visited = array() ) {
	$chain = '';
	$parent = get_term( $id, 'topics' );
	if ( is_wp_error( $parent ) )
		return $parent;

	if ( $nicename )
		$name = $parent->slug;
	else
		$name = $parent->name;
	
	if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
		$visited[] = $parent->parent;
		$chain .= get_term_parents( $parent->parent, $link, $separator, $nicename, $visited );		
	}

	if ( $link )
		$chain .= '<a href="' . esc_url( get_term_link( $parent->term_id ) ) . '">'.$name.'</a>' . $separator;
	else
		$chain .= $name.$separator;
	return $chain;
}
add_theme_support( 'post-thumbnails', array( 'post', 'news_type' ) );
function add_editor_buttons($buttons) {
	$buttons[] = 'fontselect';	
	$buttons[] = 'fontsizeselect';
	$buttons[] = 'backcolor';
	$buttons[] = 'wp_page';	
	return $buttons;
}
add_filter("mce_buttons", "add_editor_buttons");
register_nav_menus(array('header-menu' => __( '<strong>导航菜单</strong>' )));
if( function_exists('register_sidebar') ) {
	register_sidebars(1,array(
		'name' => '侧栏小工具',
		'id' => 'sidebar',
		'description'   => '',
		'before_widget' => '<section class="widget">',
		'after_widget' => '</section>',
		'before_title' => '<h3 class="sidetitle">',
		'after_title' => '</h3>'
	));
}
function exclude_page() {
	global $post;
	if ($post->post_type == 'page') {
		return true;
	} else {
		return false;
	}
}
function get_category_root_id($cat){
	$this_category = get_category($cat);
	while($this_category->category_parent){
		$this_category = get_category($this_category->category_parent);
	}
	return $this_category->term_id;
}
function get_term_root_id($term){
	$this_term = get_term( $term, 'topics' );
	while($this_term->parent){
		$this_term = get_term($this_term->parent,'topics');
	}
	return $this_term->term_id;
}
function rename_filename($filename) {
	$info = pathinfo($filename);
	$ext = empty($info['extension']) ? '' : '.' . $info['extension'];
	$name = basename($filename, $ext);
	return substr(md5($name), 0, 20) . $ext;
}
add_filter('sanitize_file_name', 'rename_filename', 10);
function post_is_in_descendant_category( $cats,$_post = null ){
	foreach ( (array) $cats as $cat ) {
		$descendants = get_term_children( (int) $cat,'category');
		if ( $descendants &&in_category( $descendants,$_post ) )
			return true;
		}
	return false;
}

function get_category_tags($args) {
	global $wpdb;
	$tags = $wpdb->get_results
		("
            SELECT DISTINCT terms2.term_id as tag_id, terms2.name as tag_name
            FROM
                $wpdb->posts as p1
                LEFT JOIN $wpdb->term_relationships as r1 ON p1.ID = r1.object_ID
                LEFT JOIN $wpdb->term_taxonomy as t1 ON r1.term_taxonomy_id = t1.term_taxonomy_id
                LEFT JOIN $wpdb->terms as terms1 ON t1.term_id = terms1.term_id,
                $wpdb->posts as p2
                LEFT JOIN $wpdb->term_relationships as r2 ON p2.ID = r2.object_ID
                LEFT JOIN $wpdb->term_taxonomy as t2 ON r2.term_taxonomy_id = t2.term_taxonomy_id
                LEFT JOIN $wpdb->terms as terms2 ON t2.term_id = terms2.term_id
            WHERE
                t1.taxonomy = 'category' AND p1.post_status = 'publish' AND terms1.term_id IN (".$args['categories'].") AND
                t2.taxonomy = 'post_tag' AND p2.post_status = 'publish'
                AND p1.ID = p2.ID
            ORDER by tag_name
			LIMIT 5
        ");
        $count = 0;
        if($tags) {
			foreach ($tags as $tag) {
				$mytag[$count] = get_term_by('id', $tag->tag_id, 'post_tag');
				$count++;
			}
        }
        else {
			$mytag = NULL;
        }       
        return $mytag;
}
function catch_that_image() {
	global $post, $posts;
	$first_img = '';
	ob_start();
	ob_end_clean();
	$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
	if(isset($matches[1][0])){
		$first_img = $matches[1][0];
	}else{
        $first_img = get_bloginfo('template_directory').'/images/no-image.jpg';
	}
	return $first_img;
}
function get_breadcrumbs(){
    global $wp_query; 
	if(is_home()) {
		echo '<a href="'. get_option('home') .'" title="'. get_option('blogname').'">首页</a>';		
	}
    if ( !is_home() ){
        echo '<a href="'. get_option('home') .'" title="'. get_option('blogname').'">首页</a>';
        if ( is_category() ){
            $catTitle = single_cat_title( "", false );
            $cat = get_cat_ID( $catTitle );
            echo '<i>&gt;</i>'. get_category_parents( $cat, TRUE, "" );
        }elseif(is_tax()){
			$term = get_term_by('slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
			echo '<i>&gt;</i>'. get_term_parents( $term->term_id, TRUE, '<i>&gt;</i>' );
		}elseif(is_tag()){
			echo '<i>&gt;</i>';
			single_tag_title();
		}
        elseif ( is_archive())
        {
            echo '<i>&gt;</i>';
			wp_title('',true);
        }
        elseif ( is_search() ) { 
            echo '<i>&gt;</i>搜索结果';
        }
        elseif ( is_404() )
        {
            echo '<i>&gt;</i>404页面不存在';
        }
        elseif ( is_single() )
        {
			if(get_post_type() == 'news_type'){				
				$terms = get_the_terms( get_the_ID() , 'topics');
				echo '<i>&gt;</i>'. get_term_parents( $terms[0]->term_id, TRUE, '' );
			}else{
				$category = get_the_category();
				$category_id = get_cat_ID( $category[0]->cat_name );
				echo '<i>&gt;</i>'. get_category_parents( $category_id, TRUE, '' );   
			}			
        }
        elseif ( is_page() )
        {
            $post = $wp_query->get_queried_object();
            if ( $post->post_parent == 0 ){
                echo '<i>&gt;</i>'.the_title('','', FALSE);
            } else {
                $title = the_title('','', FALSE);
                $ancestors = array_reverse( get_post_ancestors( $post->ID ) );
                array_push($ancestors, $post->ID);
                foreach ( $ancestors as $ancestor ){
                    if( $ancestor != end($ancestors) ){
                        echo '<i>&gt;</i><a href="'. get_permalink($ancestor) .'">'. strip_tags( apply_filters( 'single_post_title', get_the_title( $ancestor ) ) ) .'</a>';
                    } else {
                        echo '<i>&gt;</i>'. strip_tags( apply_filters( 'single_post_title', get_the_title( $ancestor ) ) );
                    }
                }
            }
        }else{
			echo '<i>&gt;</i>';
			wp_title('',true);
		}       
    }
}
function tagtext(){
	global $post;
	$gettags = get_the_tags($post->ID);
	if ($gettags) {
		foreach ($gettags as $tag) {
		$posttag[] = $tag->name;
	}
	$tags = implode( ',', $posttag );
		return $tags;
	}
}
function get_tag_id() {
    $current_tag = single_tag_title('', false);
    $tags = get_tags();
    foreach($tags as $tag) {
        if($tag->name == $current_tag) return $tag->term_id;
    }
}
function leonhere_pagenavi($range = 9){
	global $paged, $wp_query;
	if ( !$max_page ) {$max_page = $wp_query->max_num_pages;}
	if($max_page > 1){if(!$paged){$paged = 1;}
	if($paged != 1){echo "<a href='" . get_pagenum_link(1) . "' class='extend' title='跳转到首页'> 首页 </a>";}
	previous_posts_link(' 上一页 ');
    if($max_page > $range){
		if($paged < $range){for($i = 1; $i <= ($range + 1); $i++){echo "<a href='" . get_pagenum_link($i) ."'";
		if($i==$paged)echo " class='oncurrent'";echo ">$i</a>";}}
    elseif($paged >= ($max_page - ceil(($range/2)))){
		for($i = $max_page - $range; $i <= $max_page; $i++){echo "<a href='" . get_pagenum_link($i) ."'";
		if($i==$paged)echo " class='oncurrent'";echo ">$i</a>";}}
	elseif($paged >= $range && $paged < ($max_page - ceil(($range/2)))){
		for($i = ($paged - ceil($range/2)); $i <= ($paged + ceil(($range/2))); $i++){echo "<a href='" . get_pagenum_link($i) ."'";if($i==$paged) echo " class='oncurrent'";echo ">$i</a>";}}}
    else{for($i = 1; $i <= $max_page; $i++){echo "<a href='" . get_pagenum_link($i) ."'";
    if($i==$paged)echo " class='oncurrent'";echo ">$i</a>";}}
	next_posts_link(' 下一页 ');
    if($paged != $max_page){echo "<a href='" . get_pagenum_link($max_page) . "' class='extend' title='跳转到最后一页'> 末页 </a>";}}
}
function copyrightDate() {
	global $wpdb;
	$copyright_dates = $wpdb->get_results("
		SELECT
			YEAR(min(post_date_gmt)) AS firstdate,
			YEAR(max(post_date_gmt)) AS lastdate
		FROM
			$wpdb->posts
		WHERE post_status = 'publish'
	");
	if($copyright_dates) {
		$date = date('Y-m-d');
		$date = explode('-', $date);
		$copyright = "Copyright &copy; " . $copyright_dates[0]->firstdate;
		if($copyright_dates[0]->firstdate != $date[0]) {
			$copyright .= '-' . $date[0];
		}
		echo $copyright;
	}
}
function getToday(){
	$today = getdate();
	$query = new WP_Query('post_type=post&year=' . $today["year"] . '&monthnum=' . $today["mon"] . '&day=' . $today["mday"]);
	$getNum = $query->found_posts;
	echo $getNum;
}
function customFormat($mimes = array()) {
// 添加mobileconfig类型，如果需要添加其他类型，例如SVG，只需在下面加入一行 $mimes['svg'] = "text/svg";即可
$mimes['apk'] = "text/apk";
$mimes['ipa'] = "text/ipa";
return $mimes;
}
add_action('upload_mimes', 'customFormat');

//添加 collection class
require 'include/at_collection.php';